<?php
header('Content-Type:text/html;charset=utf-8');
// require_once('./locl.php');
function pub($con){
    $link=mysql_connect($con['host'],$con['user'],$con['pwd']);
    if(!$link){
        die('错误号：'.mysql_errno().'错误信息：'.mysql_error());
    }
    mysql_select_db($con['db']);
    mysql_set_charset($con['charset']);
}

function select2($con,$table,$where=''){
    pub($con);
    $sql="select * from {$table} {$where}";
    $res=mysql_query($sql);
    $arr=array();
    while($row = mysql_fetch_assoc($res)){
        $arr[]=$row;
        // var_dump($arr);die;
    }
    mysql_free_result($res);
    mysql_close();
    return $arr;
}

function select($con,$table,$where='',$ye=4){
    // echo $uname;
    pub($con);   
    $p=isset($_GET['page'])?$_GET['page']:'1';
    $shang=$p-1;
    $xia=$p+1;
    $pian=($p-1)*$ye;
    $sql="select * from {$table} {$where} limit {$pian},$ye";
    $sql2="select count(*) as shu from {$table} {$where}";
    $res=mysql_query($sql);
    $res2=mysql_query($sql2);
    $arr=array();
    while($row = mysql_fetch_assoc($res)){
        $arr[]=$row;
        // var_dump($arr);die;
    }
    $row2=mysql_fetch_assoc($res2);
    $zongye=ceil($row2['shu']/$ye);   
    if($shang<1){
        $shang=1;
    }
    if($xia>$zongye){
        $xia=$zongye;
    }    
    $_SESSION['page']=$p;  
    $_SESSION['zongye']=$zongye;
    $_SESSION['shang']=$shang;
    $_SESSION['xia']=$xia;
    mysql_free_result($res);
    mysql_close();
    return $arr;
}


function delete($con,$table,$id){
pub($con);
$sql="delete from {$table} where `id`={$id}";
$res=mysql_query($sql); 
mysql_close();
return $res;
}


function insert($con,$table,$post=''){  
    if($post==''){
        $post=$_POST;
    }
pub($con);
$filed='`'.implode('`,`',array_keys($post)).'`';
$values="'".implode("','",$post)."'";
$sql="insert into {$table} ($filed) values ($values)";
$res=mysql_query($sql);
mysql_close();
return $res;
}

function update($con,$table,$id,$post){
pub($con);
$up='';
foreach($post as $k => $v){
    $up .="`{$k}`='{$v}',";
}
$up1=rtrim($up,',');
$sql="update {$table} set $up1 where `id`={$id}";
$res1=mysql_query($sql);
mysql_close();
return $res1;
}